package com.jpush.product.controller;

import brave.Span;
import brave.Tracer;
import brave.propagation.TraceContext;
import com.alibaba.excel.EasyExcel;
import com.jpush.product.listener.UploadDataListener;
import com.jpush.product.pojo.V1PsdAppTopDetailReqDTO;
import com.jpush.product.pojo.V1PsdAppTopImeiDetailRespDTO;
import com.jpush.product.pojo.V1PsdNestAppCrowdCreateReqDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/push/product")
public class ProductController {
    @Autowired
    private Tracer tracer;

    private Logger log = LoggerFactory.getLogger(ProductController.class);

    @GetMapping("/error")
    public String error() {
        int i = 5 / 0;
        return "error";
    }

    @GetMapping("/gateway")
    public String gateway(@RequestParam("id") String id) {
        System.out.println(id);
        return "gateway";
    }
    @GetMapping("/")
    public String index() {
        Span span = tracer.currentSpan();
        TraceContext context = span.context();

        Set<String> set = new HashSet<>();
        for (int i = 0; i < 1000; i++) {
            set.add(context.traceIdString());
        }

        log.info("product set size:{}",set.size());
        System.out.println("==================traceid:"+context.traceIdString());
        return "product 嘤嘤";
    }

    @PostMapping("/export")
    public void export(@RequestBody V1PsdAppTopDetailReqDTO reqDTO, HttpServletResponse response) throws IOException {
        List<V1PsdAppTopImeiDetailRespDTO> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            V1PsdAppTopImeiDetailRespDTO respDTO = new V1PsdAppTopImeiDetailRespDTO();
            respDTO.setImei("imei:" + i);
            respDTO.setImsi("imsi:" + i);
            respDTO.setMac("max:" + i);
            respDTO.setPhone("phone:" + i);

            list.add(respDTO);
        }


        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("冬马和纱", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), V1PsdAppTopImeiDetailRespDTO.class).sheet("模板").doWrite(list);
    }
    @PostMapping("/multiFile")
    public String multiFile( @RequestParam(value = "photos", required = false) MultipartFile[] file) throws IOException {

        return "multiFile";
    }


    @PostMapping("/create-task")
    public String createTask(@RequestParam(value = "reqDTO", required = false) V1PsdNestAppCrowdCreateReqDTO reqDTO, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {

        EasyExcel.read(file.getInputStream(), V1PsdAppTopImeiDetailRespDTO.class, new UploadDataListener()).sheet().doRead();
        return "microsoft upload ---";
    }
}